package com.bulletelc.apollopre.module.main.center.orders.orderComment;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.entity.OrderItemCommentBean;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;
import com.bulletelc.apollopre.widget.CustomGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by CX on 2018/2/2.
 */

public class FinishCommentAdapter extends BaseAdapter implements View.OnClickListener, RatingBar.OnRatingBarChangeListener {

    private Context context;
    private LayoutInflater inflater;

    // 点击过的item，用于焦点获取
    private int touchItemPosition = -1;
    // 订单子项bean
    private List<OrderItemCommentBean> oicList;
    private FinishCommentGridViewPicAdapter finishCommentImgAdapter;
    //
    private OnClickListener listener;

    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public FinishCommentAdapter (Context context, List<OrderItemCommentBean> oicList) {
        this.context = context;
        this.oicList = oicList;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return oicList.size();
    }

    @Override
    public Object getItem(int i) {
        return oicList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        OrderItemCommentBean bean = oicList.get(i);
        ViewHolder holder;
        if (view == null) {
            view = inflater.inflate(R.layout.order_comment_list_item, null);
            holder = new ViewHolder(view);
            holder.etContent.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    touchItemPosition = (Integer) view.getTag();
                    return false;
                }
            });
            holder.mTextWatcher = new MyTextWatcher();
            holder.etContent.addTextChangedListener(holder.mTextWatcher);
            holder.mTextWatcher.updatePosition(i);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
            holder.updatePosition(i);
        }

        holder.tvName.setText(bean.getGoodName());
        holder.tvSku.setText(bean.getGoodSkuName());
        holder.tvPrice.setText("价格：¥" + bean.getGoodPrice());
        holder.tvNum.setText("数量：" + bean.getGoodQuantity());
        if (bean.getMark() != null) {
            holder.rBarMark.setRating(Float.parseFloat(bean.getMark()));
        }
        ImageLoaderManager.getInstance().loadImage(ImageLoaderManager.getDefaultOptions(holder.ivImg, bean.getGoodPic()));

        holder.rBarMark.setOnRatingBarChangeListener(this);
        holder.rBarMark.setTag(i);
        holder.etContent.setText(oicList.get(i).getContent());
        holder.etContent.setTag(i);

        ArrayList<HashMap<String, String>> gridViewPicList = bean.getImgPath();
        if (gridViewPicList.size() == 0) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("content", "");
            gridViewPicList.add(hashMap);
        } else if (gridViewPicList.size() > 1 && gridViewPicList.size() <= 6){
            clearNullPath(gridViewPicList);
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("content", "");
            gridViewPicList.add(hashMap);
        } else if (gridViewPicList.size() > 6) {
            clearNullPath(gridViewPicList);
        }
        FinishCommentGridViewPicAdapter adapter = new FinishCommentGridViewPicAdapter(context, gridViewPicList, i);
        holder.cgvPics.setAdapter(adapter);

        if (touchItemPosition == i) {
            holder.etContent.requestFocus();
            holder.etContent.setSelection(holder.etContent.getText().length());
        } else {
            holder.etContent.clearFocus();
        }

        return view;
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
        int i = (Integer) ratingBar.getTag();
        oicList.get(i).setMark(String.valueOf((int) v));
    }

    class ViewHolder {
        ImageView ivImg;
        TextView tvName;
        TextView tvSku;
        TextView tvPrice;
        TextView tvNum;
        RatingBar rBarMark;
        EditText etContent;
        CustomGridView cgvPics;
        MyTextWatcher mTextWatcher;

        public ViewHolder(View view) {
            ivImg = view.findViewById(R.id.ivImg);
            tvName = view.findViewById(R.id.tvName);
            tvSku = view.findViewById(R.id.tvSku);
            tvPrice = view.findViewById(R.id.tvPrice);
            tvNum = view.findViewById(R.id.tvNum);
            rBarMark = view.findViewById(R.id.rBarMark);
            cgvPics = view.findViewById(R.id.gvImages);
            etContent = view.findViewById(R.id.etContent);
        }

        //动态更新TextWathcer的position
        public void updatePosition(int position) {
            mTextWatcher.updatePosition(position);
        }
    }

    class MyTextWatcher implements TextWatcher {
        //由于TextWatcher的afterTextChanged中拿不到对应的position值，所以自己创建一个子类
        private int mPosition;

        public void updatePosition(int position) {
            mPosition = position;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            oicList.get(mPosition).setContent(s.toString());
        }
    }

    interface OnClickListener {

        void onClick(int position);
    }

    /**
     * 清除空路径
     */
    public void clearNullPath(ArrayList<HashMap<String, String>> gridViewPicList) {
        for (int o = 0; o < gridViewPicList.size(); o++) {
            if (gridViewPicList.get(o).get("content").equals("")) {
                gridViewPicList.remove(o);
            }
        }
    }
}
